/**
 * 用此方法提交会超时
 * @param {string} p
 * @return {number}
 */
export var findSubstringInWraproundString = function (p) {
  if (p.length < 2) return p.length
  const memo = new Set()
  memo.add(p[0])
  let ans = 1
  let startIdx = 0
  for (let i = 1; i < p.length; i++) {
    const cur = p[i]
    const pre = p[i - 1]
    if (cur.charCodeAt() - pre.charCodeAt() === 1 || (cur === 'a' && pre === 'z')) {
      ans += getNewSubStrsLen(p, startIdx, i, memo)
    } else {
      if (!memo.has(cur)) {
        memo.add(cur)
        ans += 1
      }
      startIdx = i
    }
  }
  return ans
}

function getNewSubStrsLen(p, s, e, memo) {
  let ans = 0
  for (let i = e; i >= s; i--) {
    const subStr = p.slice(i, e + 1)
    if (!memo.has(subStr)) {
      memo.add(subStr)
      ans++
    }
  }
  return ans
}
